<template>
  <a-drawer
    :title="title"
    :maskClosable="true"
    :width="drawerWidth"
    placement="right"
    :closable="true"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @close="handleCancel"
  >
    <template slot="title">
      <div style="width: 100%">
        <span>{{ title }}</span>
        <span style="display: inline-block; width: calc(100% - 51px); padding-right: 10px; text-align: right">
          <a-button @click="toggleScreen" icon="appstore" style="height: 20px; width: 20px; border: 0px"></a-button>
        </span>
      </div>
    </template>

    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="model" :rules="validatorRules">
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" prop="title" required label="文章标题">
          <a-input placeholder="请输入文章标题" v-model="model.title" :maxLength="50" />
        </a-form-model-item>
        <a-form-model-item label="形象图" :labelCol="labelCol" :wrapperCol="wrapperCol" required prop="image">
          <j-image-upload
            class="avatar-uploader"
            text="上传"
            v-model="model.image"
            :is-multiple="true"
          ></j-image-upload>
        </a-form-model-item>
        <a-form-model-item
          v-if="model.classId == 35 || model.audio"
          label="音频"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
        >
          <audio controls>
            <source ref="source" />
          </audio>
        </a-form-model-item>
        <a-row style="margin-top: -20px" v-if="model.classId == 35">
          <a-col offset="5">
            <a-upload name="file" :action="uploadAction" @change="handleChange">
              <a-button>选择文件</a-button>
            </a-upload>
          </a-col>
        </a-row>
        <a-form-model-item label="文章置顶" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-radio-group v-model="model.topMark">
            <a-radio :value="1"> 是 </a-radio>
            <a-radio :value="2"> 否 </a-radio>
          </a-radio-group>
        </a-form-model-item>
        <a-form-model-item :labelCol="labelCol" :wrapperCol="wrapperCol" required prop="content" label="文章内容">
          <j-editor v-model="model.content" />
        </a-form-model-item>
        <a-form-model-item label="访问量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number :min="0" v-model="model.views"></a-input-number>
        </a-form-model-item>
        <a-form-model-item label="自定义访问量" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input-number :min="0" v-model="model.customViews"></a-input-number>
        </a-form-model-item>
        <a-form-model-item label="标签" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-checkbox-group v-model="keywords" name="checkboxgroup" :options="tagsList" @change="onChange" />
        </a-form-model-item>
      </a-form-model>
    </a-spin>

    <div class="drawer-bootom-button" v-show="!disableSubmit">
      <a-popconfirm title="确定放弃编辑？" @confirm="handleCancel" okText="确定" cancelText="取消">
        <a-button style="margin-right: 0.8rem">取消</a-button>
      </a-popconfirm>
      <a-button @click="handleOk" type="primary" :loading="confirmLoading">提交</a-button>
    </div>
  </a-drawer>
</template>

<script>
import Vue from 'vue'
import { USER_INFO } from '@/store/mutation-types'
import { getAction, postAction } from '@/api/manage'

export default {
  name: 'ServiceModal',
  data() {
    return {
      drawerWidth: 900,
      disableSubmit: false,
      value: 1,
      title: '操作',
      visible: false,
      model: {},
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: false,
      validatorRules: {
        title: [{ required: true, message: '请输入文章标题!' }],
        image: [{ required: true, message: '请上传形象图!' }],
        content: [{ required: true, message: '请输入文章内容!' }]
      },
      keywords: '',
      tagsList: [
        {
          label: '糖尿病',
          value: 0
        },
        {
          label: '运动',
          value: 1
        },
        {
          label: '定义',
          value: 2
        },
        {
          label: '监测',
          value: 3
        },
        {
          label: '检查',
          value: 4
        },
        {
          label: '注意事项',
          value: 5
        },
        {
          label: '饮食',
          value: 6
        },
        {
          label: '低血糖',
          value: 7
        },
        {
          label: '作用',
          value: 8
        },
        {
          label: '预防',
          value: 9
        }
      ],
      serviceCategory: []
    }
  },
  computed: {
    uploadAction() {
      return `${window._CONFIG['domianURL']}/sys/common/upload`
    }
  },
  created() {},
  methods: {
    add(id) {
      this.model.classId = id
      this.edit()
    },
    edit(record) {
      this.visible = true
      record && this.getDetail(record)
      this.resetScreenSize()
    },
    getDetail(id) {
      postAction('article/detail', { dataId: id }).then(res => {
        if (res.success) {
          this.model = res.result
          const tag = []
          this.tagsList.forEach(v => {
            if (this.model.keywords.split(',').includes(v.label)) {
              tag.push(v.value)
            }
          })
          this.keywords = tag || []
        } else {
          console.log(res.message)
        }
      })
    },
    handleChange(file) {
      if (file.file.status == 'done') {
        this.model.audio = file.file.response.message
        this.$refs.source.src = file.file.response.message
        console.log(this.$refs.source)
      }
    },
    // 根据屏幕变化,设置抽屉尺寸
    resetScreenSize() {
      let screenWidth = document.body.clientWidth
      if (screenWidth < 500) {
        this.drawerWidth = screenWidth
      } else {
        this.drawerWidth = 900
      }
    },
    //窗口最大化切换
    toggleScreen() {
      if (this.modaltoggleFlag) {
        this.modalWidth = window.innerWidth
      } else {
        this.modalWidth = 800
      }
      this.modaltoggleFlag = !this.modaltoggleFlag
    },
    onChange(v) {
      console.log(v)
    },
    // 确定
    handleOk() {
      const that = this
      // 触发表单验证
      that.$refs.form.validate(valid => {
        if (valid) {
          that.confirmLoading = true
          this.model.courseType = 1
          const tag = []
          this.tagsList.forEach(v => {
            if (this.keywords.includes(v.value)) {
              tag.push(v.label)
            }
          })
          this.model.keywords = tag.join(',')
          postAction('article/detail/manage', that.model)
            .then(res => {
              console.log('res=======', res)
              if (res.success) {
                that.$message.success(res.message)
                that.$emit('ok')
              } else {
                that.$message.warning(res.message)
              }
            })
            .finally(() => {
              that.confirmLoading = false
              that.close()
            })
        } else {
          return false
        }
      })
    },
    // 关闭
    handleCancel() {
      this.close()
    },
    close() {
      this.$emit('close')
      this.visible = false
      this.model = {}
      this.disableSubmit = false
      this.$refs.form.resetFields()
    }
  }
}
</script>

<style scoped>
.avatar-uploader > .ant-upload {
  width: 104px;
  height: 104px;
}
.ant-upload-select-picture-card i {
  font-size: 49px;
  color: #999;
}

.ant-upload-select-picture-card .ant-upload-text {
  margin-top: 8px;
  color: #666;
}

.ant-table-tbody .ant-table-row td {
  padding-top: 10px;
  padding-bottom: 10px;
}

.drawer-bootom-button {
  position: absolute;
  bottom: -8px;
  width: 100%;
  border-top: 1px solid #e8e8e8;
  padding: 10px 16px;
  text-align: right;
  left: 0;
  background: #fff;
  border-radius: 0 0 2px 2px;
}
</style>

